package com.lex.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lex.common.api.CommonResult;
import com.lex.data.admin.mapper.LexCmsDeptMapper;
import com.lex.data.admin.mapper.LexCmsUsersMapper;
import com.lex.data.admin.model.LexCmsDept;
import com.lex.data.admin.model.LexCmsUsers;
import com.lex.entity.dept.AddDeptDto;
import com.lex.entity.dept.DeptListVo;
import com.lex.entity.dept.UpdateDeptDto;
import com.lex.entity.params.DeptQueryParams;
import com.lex.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private LexCmsDeptMapper lexCmsDeptMapper;

    @Autowired
    private LexCmsUsersMapper lexCmsUsersMapper;

    /**
     * 构建部门树，递归调用，用来返回给前端
     * @param deptList
     * @param parentId
     * @return
     */
    private List<DeptListVo> buildPlatformDeptTree(List<LexCmsDept> deptList,Long parentId){
        List<DeptListVo> treeList = new ArrayList<DeptListVo>();
        for (LexCmsDept dept : deptList) {
            DeptListVo menuDto = new DeptListVo();
            menuDto.setId(dept.getId());
            menuDto.setName(dept.getDeptName());
            menuDto.setOrderNo(dept.getOrderNum());
            menuDto.setRemark(dept.getRemark());
            menuDto.setCreateTime(String.valueOf(dept.getCreateTime()));
            menuDto.setStatus(dept.getStatus());
            menuDto.setParentId(dept.getParentId());
            menuDto.setKey(dept.getId());
            menuDto.setValue(dept.getId());
            menuDto.setLabel(dept.getDeptName());
            if (dept.getParentId().equals(parentId)) {
                menuDto.setChildren(buildPlatformDeptTree(deptList, dept.getId()).isEmpty()
                        ? null
                        : buildPlatformDeptTree(deptList, dept.getId()));
                treeList.add(menuDto);
            }
        }
        return treeList;
    }
    /**
     * 获取部门列表
     * @param queryParams
     * @return
     */
    @Override
    public CommonResult getDeptList(DeptQueryParams queryParams) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (queryParams.getDeptName() != null){
            queryWrapper.like("dept_name",queryParams.getDeptName());
        }
        if (queryParams.getStatus() != null){
            queryWrapper.eq("status",queryParams.getStatus());
        }
        List<LexCmsDept> list = lexCmsDeptMapper.selectList(queryWrapper);
        List<DeptListVo> treeList = new ArrayList<>();
        if(list.size() == 1){
            //直接返回封装数据
            DeptListVo menuDto = new DeptListVo();
            menuDto.setId(list.get(0).getId());
            menuDto.setName(list.get(0).getDeptName());
            menuDto.setOrderNo(list.get(0).getOrderNum());
            menuDto.setRemark(list.get(0).getRemark());
            menuDto.setCreateTime(String.valueOf(list.get(0).getCreateTime()));
            menuDto.setStatus(list.get(0).getStatus());
            menuDto.setParentId(list.get(0).getParentId());
            menuDto.setKey(list.get(0).getId());
            menuDto.setValue(list.get(0).getId());
            menuDto.setLabel(list.get(0).getDeptName());
            treeList.add(menuDto);
        }else{
            treeList = buildPlatformDeptTree(list,0L);
        }
        return CommonResult.success(treeList);
    }

    /**
     * 添加部门
     * @param addDeptDto
     * @return
     */
    @Override
    public CommonResult addDept(AddDeptDto addDeptDto) {
        LexCmsDept lexCmsDept = new LexCmsDept();
        lexCmsDept.setDeptName(addDeptDto.getName());
        lexCmsDept.setOrderNum(addDeptDto.getOrderNum());
        lexCmsDept.setParentId(addDeptDto.getParentId());
        lexCmsDept.setRemark(addDeptDto.getRemark());
        lexCmsDept.setStatus(addDeptDto.getStatus());
        int is_success = lexCmsDeptMapper.insert(lexCmsDept);
        return is_success > 0 ? CommonResult.success("添加成功") : CommonResult.failed("添加失败");
    }

    /**
     * 修改部门
     * @param updateDeptDto
     * @return
     */
    @Override
    public CommonResult updateDept(UpdateDeptDto updateDeptDto) {
        LexCmsDept lexCmsDept = new LexCmsDept();
        lexCmsDept.setId(updateDeptDto.getId());
        lexCmsDept.setDeptName(updateDeptDto.getName());
        lexCmsDept.setOrderNum(updateDeptDto.getOrderNum());
        lexCmsDept.setParentId(updateDeptDto.getParentId());
        lexCmsDept.setRemark(updateDeptDto.getRemark());
        //获取当前时间
        long time = System.currentTimeMillis();
        Timestamp timestamp = new Timestamp(time);
        int is_success = lexCmsDeptMapper.updateById(lexCmsDept);
        return is_success > 0 ? CommonResult.success("修改成功") : CommonResult.failed("修改失败");
    }

    /**
     * 删除部门
     * @param deptId
     * @return
     */
    @Override
    public CommonResult deleteDept(Long deptId) {
        //判断是否有子部门
        QueryWrapper<LexCmsDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",deptId);
        List<LexCmsDept> list = lexCmsDeptMapper.selectList(queryWrapper);
        if(!list.isEmpty()){
            return CommonResult.failed("该部门下有子部门，无法删除");
        }
        //判断部门下是否有用户
        QueryWrapper<LexCmsUsers> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("dept_id",deptId);
        List<LexCmsUsers> list1 = lexCmsUsersMapper.selectList(queryWrapper1);
        if (!list1.isEmpty()){
            return CommonResult.failed("该部门下有用户，无法删除");
        }
        int is_success = lexCmsDeptMapper.deleteById(deptId);
        return is_success > 0 ? CommonResult.success("删除成功") : CommonResult.failed("删除失败");
    }
}
